package com.personal.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.personal.book.commons.AjaxResponse;
import com.personal.book.entity.Orders;
import com.personal.book.entity.Users;
import com.personal.book.service.impl.OrdersServiceImpl;
import com.personal.tools.PageData;

/**
 * 订单Servlet
 */
@WebServlet("/OrdersServlet")
public class OrdersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	OrdersServiceImpl osi = new OrdersServiceImpl();
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrdersServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 编码
		request.setCharacterEncoding("utf-8");
		
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
			
		}
		//根据订单编号插叙
		if("queryByUserName".equals(op)) {
			doqueryByUserName(request,response);
			System.out.println(op);
		}else if ("del".equals(op)) {
			doAjaxDel(request,response);
			
		}else if("search".equals(op)) {
			doSearch(request,response);
		}
	}

	private void doSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");

		//先获取请求参数的值
		int search = 0;
		
		if(request.getParameter("search")!=null)
		{
			search = Integer.parseInt(request.getParameter("search"));
		}
		List<Orders> list = osi.getOrdersById(search);
		request.setAttribute("lsit", list);
		request.getRequestDispatcher("orders.jsp").forward(request, response);
	}
	
	private void doAjaxDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");

		//先获取请求参数的值
		int orderId = 0;
		
		if(request.getParameter("orderId")!=null)
		{
			orderId = Integer.parseInt(request.getParameter("orderId"));
		}
		
		// 2 调用 service的方法
	    boolean flag= osi.delOrders(orderId);

		AjaxResponse res = new AjaxResponse(flag+"", 200);
		// 3将list转换为json格式的字符串
		Gson gson = new Gson();
		String jsonString = gson.toJson(res);

		// 4 响应
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.print(jsonString);

		// 5.关闭
		outPrintWriter.close();
	}


	/**
	 * 根据用户名查询订单
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doqueryByUserName(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		String userName = "";
		// 2获取请求参数
		if (request.getParameter("userName") != null) {
			userName = request.getParameter("userName");
		}
		// 页码默认值为1
		int page = 1;
		// 每页记录数默认为10
		int pageSize = 2;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}

		// 3 调用 Service的方法
		PageData<Orders> pd = osi.getOrdersByName(page, pageSize, userName);
		
		// 4传递PageData对象给jsp，此时传递的是pageData的对象
		request.setAttribute("pd", pd);

		// 4.转发回来的时候，我们将查询关键字也存储在request范围内
		request.setAttribute("userName", userName);

		// 5 转发
		request.getRequestDispatcher("adminOrders.jsp").forward(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
